package com.itboye.hutouben.activity.message;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.itboye.hutouben.R;
import com.itboye.hutouben.adapter.MessageAdapter;
import com.itboye.hutouben.base.BaseActivity;
import com.itboye.hutouben.base.MyApplcation;
import com.itboye.hutouben.base.ptr.BasePtr;
import com.itboye.hutouben.bean.SystemMessgeBean;
import com.itboye.hutouben.presenter.SysTemMessgelPresenter;
import com.itboye.hutouben.util.ByAlert;
import com.itboye.hutouben.util.IsUtilUid;
import com.itboye.hutouben.volley.ResultEntity;

import java.util.ArrayList;
import java.util.List;
import java.util.Observable;
import java.util.Observer;

import in.srain.cube.views.ptr.PtrDefaultHandler2;
import in.srain.cube.views.ptr.PtrFrameLayout;

/**
 * 系统消息
 */
public class SystemMessActivity extends BaseActivity implements Observer{
    TextView add_shap_title_tv;
    ImageView close_icon;
    PtrFrameLayout commend_anchor_ptr;
    private ListView messgeListview;
    private SysTemMessgelPresenter presenter;
    private List<SystemMessgeBean.ListEntity> list;

    private MessageAdapter adapter;
    int pageIndex = 1;

    LinearLayout kong_layoout;
    TextView kong_text;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_system_mess);
        BasePtr.setPagedPtrStyle(commend_anchor_ptr);//设置上拉下拉刷新
        list=new ArrayList<SystemMessgeBean.ListEntity>();
        add_shap_title_tv.setText(getString(R.string.system_message));
        presenter=new SysTemMessgelPresenter(this);
        showProgressDialog("加载中,请稍后", true);
        presenter.onSysTemMessge(IsUtilUid.isUid(),"6042",pageIndex+"","10");
        adapter=new MessageAdapter(MyApplcation.ctx,list);
        messgeListview.setAdapter(adapter);
        commend_anchor_ptr.setPtrHandler(new PtrDefaultHandler2() {
            @Override
            public void onLoadMoreBegin(PtrFrameLayout frame) {
                //  shopMallSearchBean.setKeyword("");
                pageIndex++;
                presenter.onSysTemMessge(IsUtilUid.isUid(),"6042",pageIndex+"","10");
            }

            @Override
            public void onRefreshBegin(PtrFrameLayout frame) {
                presenter.onSysTemMessge(IsUtilUid.isUid(),"6042",pageIndex+"","10");
            }
        });

    }

    @Override
    protected void onInit() {

    }

    @Override
    protected void onReadIntent(Bundle bundle) {

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.close_icon:
                finish();
                break;
            default:
                break;
        }
    }

    @Override
    public void update(Observable observable, Object data) {

        ResultEntity entity=handlerError(data);
        if (entity != null) {
            IsUtilUid.tianjia(entity);
            if (entity.getEventType()==presenter.SystemMessge_success){
                if(pageIndex==1){
                    list.clear();
                }
                commend_anchor_ptr.refreshComplete();//设置刷新完成
                SystemMessgeBean bean= (SystemMessgeBean) entity.getData();
                list.addAll(bean.getList());

                if (list.size() > 0) {

                    adapter.notifyDataSetChanged();
                    messgeListview.setVisibility(View.VISIBLE);
                    kong_layoout.setVisibility(View.GONE);

                } else {
                    if(pageIndex==1){
                        messgeListview.setVisibility(View.GONE);
                        commend_anchor_ptr.setVisibility(View.GONE);
                        kong_layoout.setVisibility(View.VISIBLE);
                        kong_text.setText("消息为空");
                    }
                }

            }
            if (entity.getEventType()==presenter.SystemMessge_fail){
                ByAlert.alert(entity.getData());
            }
        }
        try {
            closeProgressDialog();
        } catch (Exception e) {
        }
    }

}
